package com.xtx.air.web.interceptor;

import com.google.code.jcaptcha4struts2.core.PluginConstants;
import com.google.code.jcaptcha4struts2.core.validation.JCaptchaValidator;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ValidationAware;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * User: hanyi
 * Date: 12-2-13
 * Time: 下午2:51
 */
public class XtxJCaptchaValidationIntercepter extends AbstractInterceptor {

    private static final long serialVersionUID = 3410862000200098945L;
    private static final Log LOG = LogFactory.getLog(XtxJCaptchaValidationIntercepter.class);

    /**
     * Intercepts invocation and adds a field error if captcha validation fails.
     * <p/>
     * Note that intercepting an action which does not implement {@link com.opensymphony.xwork2.ValidationAware} or an
     * action invocation which does not contain captcha information will result in an exception.
     *
     * @param invocation ActionInvocation
     * @return action forward string
     * @throws Exception                if thrown by action invocation chain
     * @throws IllegalArgumentException if the target action does not implement {@link com.opensymphony.xwork2.ValidationAware}.
     */
    public String intercept(ActionInvocation invocation) throws Exception, IllegalArgumentException {

        if (LOG.isDebugEnabled()) {
            LOG.debug("JCaptchaValidationInterceptor intercepting...");
        }

        if (!(invocation.getAction() instanceof ValidationAware)) {
            // Action does not implement ValidationAware, which is a requirement
            // if JCaptchaValidationIntercepter is being used to intercept an action.

            LOG.error("JCaptchaValidationInterceptor intercepted action which does "
                    + "not implement ValidationAware");

            throw new IllegalArgumentException("Action does not implement ValidationAware");
        }

        try {
            // Validate and add field error if fails
            if (!JCaptchaValidator.validate()) {

                if (LOG.isDebugEnabled()) {
                    LOG.debug("JCaptchaValidationInterceptor validation failed");
                }
                // Get Action Reference
                ValidationAware action = (ValidationAware) invocation.getAction();

                action.addActionError(getValidationErrorMessage());
                return Action.INPUT;
            }
        } catch (Exception e) {
            return Action.INPUT;
        }

        if (LOG.isDebugEnabled()) {
            LOG.debug("JCaptchaValidationInterceptor validation completed successfully.");
        }

        // Continue invocation
        return invocation.invoke();
    }

    /**
     * Returns the error message text to be displayed if captcha validation fails.
     * <p/>
     * Developers may override this method to provide custom messages.
     *
     * @return error message to be displayed if captcha validation fails.
     */
    protected String getValidationErrorMessage() {
        return "验证码输入有误";
    }
}
